package com.sync.data.job.department;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.sync.data.ievent.entity.BaseCostCenter;
import com.sync.data.ievent.entity.IeventBaseEmployee;
import com.sync.data.ievent.repository.BaseCostCenterRepository;
import com.sync.data.mce.entity.BpmDepartmentInfo;
import com.sync.data.mce.entity.MceBaseEmployee;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import com.sync.data.mce.repository.BpmDepartmentInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.data.RepositoryItemReader;
import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.PlatformTransactionManager;

import java.util.Collections;
import java.util.Date;

@Configuration
@EnableBatchProcessing
@RequiredArgsConstructor
public class DepartmentBatchJob {


    private final JobBuilderFactory jobBuilderFactory;

    private final StepBuilderFactory stepBuilderFactory;

    private final BpmDepartmentInfoRepository bpmDepartmentInfoRepository;

    private final BaseCostCenterRepository baseCostCenterRepository ;

    private final PlatformTransactionManager transactionManagerIevent;

    private final JpaItemWriter<BaseCostCenter> baseCostCenterJpaItemWriter;

    // 批处理大小
    private static final int CHUNK_SIZE = 100;

    @Bean
    public Job departmentSyncJob() {
        return jobBuilderFactory.get("departmentSyncJob")
                .incrementer(new RunIdIncrementer())
                .start(syncDepartmentStep())
                .build();
    }

    @Bean
    public Step syncDepartmentStep() {
        return stepBuilderFactory.get("syncDepartmentStep")
                .<BpmDepartmentInfo, BaseCostCenter>chunk(CHUNK_SIZE)
                .reader(bpmDepartmentInfoReader())
                .processor(employeeSyncProcessor())
                .writer(baseCostCenterJpaItemWriter)
                .transactionManager(transactionManagerIevent)
                .build();
    }

    public ItemReader<BpmDepartmentInfo> bpmDepartmentInfoReader() {
        RepositoryItemReader<BpmDepartmentInfo> reader = new RepositoryItemReader<>();
        reader.setRepository(bpmDepartmentInfoRepository);
        reader.setMethodName("findAllGroupByDeptId");
        reader.setSort(Collections.singletonMap("id", Sort.Direction.ASC));
        reader.setPageSize(CHUNK_SIZE);
        return reader;
    }

    public ItemProcessor<BpmDepartmentInfo, BaseCostCenter> employeeSyncProcessor() {
        return bpmDepartmentInfo -> {
            boolean exists = baseCostCenterRepository.existsByCostCenterCode(bpmDepartmentInfo.getDeptId());

            if (exists) {
                return null;
            }

            BaseCostCenter baseCostCenter = new BaseCostCenter();
            baseCostCenter.setDelFlag(0);
            baseCostCenter.setIsActive(1);
            baseCostCenter.setCostCenterCode(bpmDepartmentInfo.getDeptId());
            baseCostCenter.setCostCenterDesc(bpmDepartmentInfo.getFullName());
            baseCostCenter.setName(bpmDepartmentInfo.getFullName());
            baseCostCenter.setCostCenterName(bpmDepartmentInfo.getName());
            baseCostCenter.setBeginDate(DateUtil.parse("2025-01-01 00:00:00"));
            baseCostCenter.setEndDate(DateUtil.parse("2125-12-31 23:59:59"));
            return baseCostCenter;
        };
    }
}